package com.tongji.diy.view.component3D
{
	/**
	 * 
	 * @author xxj
	 * 
	 */
	import com.tongji.diy.model.furniturePlacementOptimization.OptimizationParameter;
	import com.tongji.diy.utils.OptimizationMath;
	
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.events.Event;
	
	import flare.basic.Scene3D;
	import flare.core.Mesh3D;
	import flare.core.Pivot3D;
	import flare.core.Texture3D;
	import flare.loaders.ColladaLoader;
	import flare.materials.Shader3D;
	import flare.materials.filters.TextureFilter;
	
	public class Furniture extends ColladaLoader
	{
		public static var furnitureIndexCount:uint = 0;
		
		public var furnitureIndex:uint;
		
		private var _length:Number = 0;
		private var _width:Number = 0;
		private var _height:Number = 0;
		private var _meshVector:Vector.<Mesh3D> = new Vector.<Mesh3D>;
		private var _furnitureClass:int = -1;
		
		
		public function Furniture(request:*, parent:Pivot3D=null, sceneContext:Scene3D=null)
		{
			super(request, parent, sceneContext);
			furnitureIndex = furnitureIndexCount;
			furnitureIndexCount++;
			this.addEventListener(Event.COMPLETE, loadComplete);
		}
		
		private function loadComplete(e:Event):void
		{
			setLWH();
			OptimizationMath.assert(_length !=0||_width != 0);
			_furnitureClass = selectClass(this.name);
		}
		
		private function setLWH():void
		{
			for(var i:int = 0; i < this.children[this.children.length - 1].children.length; i++)
			{
				if(this.children[this.children.length - 1].children[i] is Mesh3D)
				{
					var mesh:Mesh3D = this.children[this.children.length - 1].children[i] as Mesh3D;
					_meshVector.push(mesh);
					if(_width < mesh.bounds.max.x - mesh.bounds.min.x)
						_width = mesh.bounds.max.x - mesh.bounds.min.x;
					if(_length < mesh.bounds.max.z - mesh.bounds.min.z)
						_length = mesh.bounds.max.z - mesh.bounds.min.z;
					if(_height < mesh.bounds.max.y - mesh.bounds.min.y)
						_height = mesh.bounds.max.y - mesh.bounds.min.y;
				}
			}
			
			//以下是修改的函数-------
			if(!_width && !_height && !_length)
			{
				for(i = 0; i < this.children[this.children.length - 1].children[0].children.length; i++)
				{
					if(this.children[this.children.length - 1].children[0].children[i] is Mesh3D)
					{
						mesh = this.children[this.children.length - 1].children[0].children[i] as Mesh3D;
						_meshVector.push(mesh);
						if(_width < mesh.bounds.max.x - mesh.bounds.min.x)
							_width = mesh.bounds.max.x - mesh.bounds.min.x;
						if(_length < mesh.bounds.max.z - mesh.bounds.min.z)
							_length = mesh.bounds.max.z - mesh.bounds.min.z;
						if(_height < mesh.bounds.max.y - mesh.bounds.min.y)
							_height = mesh.bounds.max.y - mesh.bounds.min.y;
					}
				}
			}
			//以上是修改的函数-------
			_width = _width/10;
			_height = _height/10;
			_length = _length/10;
		}
		//根据模型的名称来选择模型的类别，该函数只是在功能初试阶段测试用，待完成后可改为从数据库获取类型
		private function selectClass(furnitureName:String):int
		{
			switch(furnitureName)
			{
				case "desk01.DAE":
					return OptimizationParameter.DESK;
					break;
				case "bed01.DAE":
					return OptimizationParameter.BED;
					break;
				case "bed02.DAE":
					return OptimizationParameter.BED;
					break;
				case "bookcase02.dae":
					return OptimizationParameter.BOOKSHELF;
					break;
				case "cabinet01.DAE":
					return OptimizationParameter.BEDSTAND;//为了测试进行修改
					break; 
				case "holistic_kitchen01.DAE":
					return OptimizationParameter.HOLISTIC_KITCHEN;
					break; 
				case "cabinet02.DAE":
					return OptimizationParameter.CABINET;
					break;
				case "cloesttool01.DAE":
					return OptimizationParameter.CLOESTTOOL;
					break;
				case "single_sofa01.DAE":
					return OptimizationParameter.SINGLE_SOFA;
					break; 
				case "muliti_sofa01.DAE":
					return OptimizationParameter.MULITI_SOFA;
					break; 
				case "muliti_sofa02.DAE":
					return OptimizationParameter.MULITI_SOFA;
					break; 
				case "TV01.DAE":
					return OptimizationParameter.TV;
					break; 
				case "chair01.DAE":
					return OptimizationParameter.OFFICE_CHAIR;//修改过
					break;
				case "chair02.DAE":
					return OptimizationParameter.CHAIR;
					break; 
				case "side_table01.DAE":
					return OptimizationParameter.SIDE_TABLE;
					break; 
				case "desk02.DAE":
					return OptimizationParameter.DESK;
					break; 
				case "wooddesk02.dae":
					return OptimizationParameter.DESK;
					break; 
				case "desk03.DAE":
					return OptimizationParameter.DINING_TABLE;
					break; 
				case "other01.DAE":
					return OptimizationParameter.OTHER;
					break;
				case "cabinet03.DAE":
					return OptimizationParameter.CABINET;
					break;
				case "cabinet04.DAE":
					return OptimizationParameter.TV_RACK;//修改过
					break;
				case "muliti_sofa03.DAE":
					return OptimizationParameter.MULITI_SOFA;
					break;
				case "TV02.DAE":
					return OptimizationParameter.TV;
					break;
				case "wash_blow01.DAE":
					return OptimizationParameter.WASH_BLOW;
					break;
				case "chair03.DAE":
					return OptimizationParameter.CHAIR;
					break;
				case "bedstand01.DAE":
					return OptimizationParameter.BEDSTAND;
					break;
				case "fireplace01.DAE":
					return OptimizationParameter.FIREPLACE;
					break;
				case "ceiling_light.DAE":
					return OptimizationParameter.CEILING_LAMP;
					break;
				case "dustbin01.dae":
					return OptimizationParameter.BIN;
					break;
				case "laptop01.dae":
					return OptimizationParameter.LAPTOP;
					break;
				case "babybed01.DAE":
					return OptimizationParameter.BABAYBED;
					break;
				case "light01.dae":
					return OptimizationParameter.FLOOR_LAMP;
					break;
				case "oil_painting01.DAE":
					return OptimizationParameter.OILPAINT;
					break;
				case "smallairconditioner01.dae":
					return OptimizationParameter.AIRCONDITION;
					break;
				default:
					return -1;		
			}
			//trace("furnitureName",furnitureName);
		}		
		
		public function setOverlap(color:uint = 0xf0ff0000):void//怀疑surface里面的filter有问题，貌似不能对单一的一个filters进行操作，添加一个将会对所有filter都有影响
		{
			for(var i:int = 0; i < this._meshVector.length;i++)
			{
				var tempMesh:Mesh3D = _meshVector[i];
				for(var j:int = 0; j < tempMesh.surfaces.length; j++)
				{
					var material:Shader3D = tempMesh.surfaces[j].material as Shader3D;
					if(material.filters.length < 2)
					{
						var bitMap:BitmapData = new BitmapData(50,50,true, color);
						var newFilter:TextureFilter = new TextureFilter(new Texture3D(bitMap,true),1);
						newFilter.blendMode = BlendMode.MULTIPLY;
						material.filters.push(newFilter);
						material.build();
					}
				}
			}
//			for (var k:int = 0; k<this.children[this.children.length - 1].children.length; k++)	
//			{
//				if (this.children[this.children.length - 1].children[k] is Mesh3D)
//				{
//					var tempMesh:Mesh3D = this.children[this.children.length - 1].children[k] as Mesh3D;
//					if(tempMesh.surfaces.length > 0)
//					{
//						var material:Shader3D = tempMesh.surfaces[0].material as Shader3D;
//						var bitMap:BitmapData = new BitmapData(50,50,true, color);
//						var newFilter:TextureFilter = new TextureFilter(new Texture3D(bitMap,true),1);
//						newFilter.blendMode = BlendMode.MULTIPLY;
//						if(material.filters.length)
//						{
//							material.filters.push(newFilter);
//							material.build();
//						}
//					}
//				}
//			}
		}
		public function removeOverlap():void
		{
			for(var i:int = 0; i < this._meshVector.length;i++)
			{
				var tempMesh:Mesh3D = _meshVector[i];
				for(var j:int = 0; j < tempMesh.surfaces.length; j++)
				{
					var material:Shader3D = tempMesh.surfaces[j].material as Shader3D;
					while(material.filters.length > 1)
						material.filters.pop();
					material.build();						
				}
			}
			/*for (var k:int = 0; k < this.children[this.children.length - 1].children.length; k++)	
			{
				if (this.children[this.children.length - 1].children[k] is Mesh3D)
				{
					var tempMesh:Mesh3D = this.children[this.children.length - 1].children[k] as Mesh3D;
					if(tempMesh.surfaces.length > 0)
					{
						var material:Shader3D = tempMesh.surfaces[0].material as Shader3D;
						if(material.filters.length > 1)
						{
							material.filters.pop();
						}
						material.build();
						
					}
				}
			}*/
		}
		
		
		public function setTransparent():void
		{
			for(var i:int = 0; i < _meshVector.length; i++)
			{
				for(var j:int = 0; j < _meshVector[i].surfaces.length; j++)
				{
					var material:Shader3D = _meshVector[i].surfaces[j].material as Shader3D;
					if(material.filters.length)
					{
						material.filters[0].alpha = 0.5;
						material.build();
					}
				}
			}
		}
		
		public function setSolid():void
		{
			for(var i:int = 0; i < _meshVector.length; i++)
			{
				for(var j:int = 0; j < _meshVector[i].surfaces.length; j++)
				{
					var material:Shader3D = _meshVector[i].surfaces[j].material as Shader3D;
					if(material.filters.length)
					{
						material.filters[0].alpha = 1;
						material.build();
					}
				}
			}
		}
		
		public function meshChildAddListener(eventType:String, func:Function):void
		{
			for(var i:int = 0; i < _meshVector.length; i++)
			{
				_meshVector[i].addEventListener(eventType, func);
			}
		}
		
		public function meshChildRemoveListener(eventType:String, func:Function):void
		{
			for(var i:int = 0; i < _meshVector.length; i++)
			{
				_meshVector[i].removeEventListener(eventType, func);
			}
		}
		
		public function get length():Number
		{
			return _length;
		}
		
		public function get width():Number
		{
			return _width;
		}
		
		public function get height():Number
		{
			return _height;
		}
		public function get rotation():Number
		{
			return (this.getRotation().x == 0) ? this.getRotation().y : 180 - this.getRotation().y;
		}
		public function get meshVector():Vector.<Mesh3D>
		{
			return _meshVector;
		}
		public function get furnitureClass():int
		{
			return _furnitureClass;
		}
	}
}